package com.qmx.qmxapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.bean.JoinMeeting;
import com.qmx.qmxapp.bean.LzyResponse;
import com.qmx.qmxapp.bean.MeetingListModel;
import com.qmx.qmxapp.callback.JsonCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;
import com.qmx.qmxapp.token.TokenManager;
import com.tencent.ilivesdk.core.ILiveLoginManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.realm.Realm;

/**
 * 发起会议
 */
public class InitiateMeetingActivity extends BaseActivity {
    @BindView(R.id.bt_start_meeting)
    Button bt_start_meeting;
    @BindView(R.id.tv_mine_yun_meeting_number)
    TextView tv_mine_yun_meeting_number;
    private Gson mGson = new Gson();
    private String roomId = "";
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_initiate_meeting);
        ButterKnife.bind(this);
        roomId = String.valueOf(sharedPreferences.getInt(AppConstant.ROOM_ID, 0));
        tv_mine_yun_meeting_number.setText(roomId);
        realm = Realm.getDefaultInstance();

    }

    @OnClick(R.id.bt_start_meeting)
    public void onclick(View v) {
        token(realm);
        joinMeeting();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //        if (ILiveLoginManager.getInstance().isLogin()) {
        //            ILiveLoginManager.getInstance().iLiveLogout();
        //        }
    }

    public void joinMeeting() {
        OkGo.<LzyResponse<JoinMeeting>>post(AppConstant.Url + ComParamContact.Launch.PATH)
                .params(ComParamContact.Launch.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .params(ComParamContact.Launch.__TOKEN__, __TOKEN__)
                .params(ComParamContact.Launch.ROOM_ID, roomId)
                .params(ComParamContact.Launch.CLIENT_ID, sharedPreferences.getString(AppConstant.CLIENT_ID, ""))
                .params(ComParamContact.Launch.CAMERA, "")
                .params(ComParamContact.Launch.MIC, "")
                .params(ComParamContact.Launch.PASSWORD, "")
                .execute(new JsonCallback<LzyResponse<JoinMeeting>>() {

                    @Override
                    public void onSuccess(Response<LzyResponse<JoinMeeting>> response) {
                        String json = mGson.toJson(response.body().code);
                        Log.i("InitiateMeetingActivity", json);
                        //                        100：平台无此会议号 101：请输入密码 102：密码错误 103：方数不足 104：加入成功 104：短线重连
                        if (json.equals("1")) {
                            Intent intent = new Intent(InitiateMeetingActivity.this, RoomActivity.class);
                            Log.e("InitiateMeetingActivity", roomId);
                            Log.e("InitiateMeetingActi===", tv_mine_yun_meeting_number.getText().toString());
                            intent.putExtra("roomId", roomId);
                            startActivity(intent);
                        } else if (json.equals("0")) {
                            showToast(response.body().msg);
                        } else if (json.equals("102")) {
                            showToast(response.body().msg);
                        } else if (json.equals("102")) {
                            showToast(response.body().msg);
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<JoinMeeting>> response) {
                        Log.i("onError", response.getException().getMessage());
                    }
                });
    }
}
